/*
 *  WaveFunctionFactory.cpp
 *  SoundController0002
 *
 *  Created by jiro on 10/11/20.
 *  Copyright 2010 jiro music. All rights reserved.
 *
 */


#include "WaveFunctionFactory.h"

namespace sc
{

	std::vector< const WaveFunction* > WaveFunctionFactory::waveFunctions;	

	const WaveFunction& WaveFunctionFactory::CreateXSinX( double powerOfX, double cycle, bool isReverse )
	{
		const WaveFunction* function = Get( WaveFunctionXSinX( powerOfX, cycle, isReverse ) );
		if ( function != NULL ) {
			return * function;
		}
		function = new WaveFunctionXSinX( powerOfX, cycle, isReverse );
		waveFunctions.push_back( function );
		return * function;
	}
	
	const WaveFunction& WaveFunctionFactory::CreateLogXSinX( double powerOfXOfLog, double cycle, bool isReverse )
	{
		const WaveFunction* function = Get( WaveFunctionLogXSinX( powerOfXOfLog, cycle, isReverse ) );
		if ( function != NULL ) {
			return * function;
		}
		function = new WaveFunctionLogXSinX( powerOfXOfLog, cycle, isReverse );
		waveFunctions.push_back( function );
		return * function;
	}

	const WaveFunction& WaveFunctionFactory::CreateSinXVariableCycle(
		Line::Type cycleVaryLineType, 
		double startCycle, double endCycle, double xAtStartCycle, double xAtEndCycle )
	{
		const WaveFunction* function = 
			Get( WaveFunctionSinXVariableCycle( cycleVaryLineType, startCycle, endCycle, xAtStartCycle, xAtEndCycle ) );
		if ( function != NULL ) {
			return * function;
		}
		function = new WaveFunctionSinXVariableCycle( cycleVaryLineType, startCycle, endCycle, xAtStartCycle, xAtEndCycle );
		waveFunctions.push_back( function );
		return * function;
	}

	const WaveFunction& WaveFunctionFactory::CreateSinPowerOfX( double powerOfX, double cycle )
	{
		const WaveFunction* function = Get( WaveFunctionSinPowerOfX( powerOfX, cycle ) );
		if ( function != NULL ) {
			return * function;
		}
		function = new WaveFunctionSinPowerOfX( powerOfX, cycle );
		waveFunctions.push_back( function );
		return * function;
	}

	bool WaveFunctionFactory::IsExist( const WaveFunction& function ) 
	{
		if ( Get( function ) == NULL ) {
			return false;
		}
		return true;
	}

	const WaveFunction* WaveFunctionFactory::Get( const WaveFunction& function ) 
	{
		size_t count = waveFunctions.size();
		for ( int i = 0; i < count; i++ ) {
			if ( function == *( waveFunctions[ i  ] ) ) {
				return waveFunctions[ i ];
			}
		}
		return NULL;
	}

}